#include "list.h"


#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QPushButton>
#include <QListWidget>
#include <QStringList>

#include "animeview.h"
#include "flowcontroler.h"


List::List(QWidget *parent) : QWidget(parent)
{
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    m_tagGroup = new  QButtonGroup(this);
    m_tagGroup->setExclusive(false);
    m_yearGroup = new  QButtonGroup(this);
    m_yearGroup->setExclusive(false);


    int tagIndex = 0;
    // 第一行：大分类
    QStringList mainCategories = {"全部", "原创", "漫画改", "小说改", "游戏改", "特摄", "热血", "穿越", "奇幻", "战斗", "搞笑", "日常", "科幻", "治愈", "校园", "泡面", "恋爱"};
    QHBoxLayout *mainCategoryLayout = new QHBoxLayout;
    for (const QString &category : mainCategories) {
        QPushButton *button = new QPushButton(category);
        button->setMinimumHeight(25);
        button->setCheckable(true);
        button->setStyleSheet("QPushButton { border: none; background-color: white; }"
                              "QPushButton:hover { background-color: lightblue; }"
                              "QPushButton:checked { background-color: rgb(154,205,50);}");
        mainCategoryLayout->addWidget(button);
        if(category != "全部")
        {
            m_tagGroup->addButton(button, tagIndex++);
        }
        else
        {
            btnAll1 = button;
            btnAll1->setChecked(true);
        }

        connect(button, &QPushButton::clicked, this, [this, category]() { onCategoryClicked(category); });
    }
    mainLayout->addLayout(mainCategoryLayout);

    // 第二行：子分类
    QStringList subCategories = {"少女", "魔法", "冒险", "历史", "架空", "运动", "励志", "音乐", "推理", "社团", "智斗", "催泪", "美食", "偶像", "乙女", "职场"};
    QHBoxLayout *subCategoryLayout = new QHBoxLayout;
    for (const QString &category : subCategories) {
        QPushButton *button = new QPushButton(category);
        button->setMinimumHeight(25);
        button->setCheckable(true);
        button->setStyleSheet("QPushButton { border: none; background-color: white; }"
                              "QPushButton:hover { background-color: lightblue; }"
                              "QPushButton:checked { background-color: rgb(154,205,50);}");
        subCategoryLayout->addWidget(button);
        m_tagGroup->addButton(button, tagIndex++);
        connect(button, &QPushButton::clicked, this, [this, category]() { onCategoryClicked(category); });
    }
    mainLayout->addLayout(subCategoryLayout);

    int yearIndex = 0;
    // 第三行：年份
    QStringList years = {"全部", "2024", "2023", "2022", "2021", "2020", "2019", "2018", "2017", "2016", "2015", "2014"};
    QHBoxLayout *yearLayout = new QHBoxLayout;
    for (const QString &year : years) {
        QPushButton *button = new QPushButton(year);
        button->setMinimumHeight(25);
        button->setCheckable(true);
        button->setStyleSheet("QPushButton { border: none; background-color: white; }"
                              "QPushButton:hover { background-color: lightblue; }"
                              "QPushButton:checked { background-color: rgb(154,205,50);}");
        yearLayout->addWidget(button);
        if(year  != "全部")
        {
            m_yearGroup->addButton(button, yearIndex++);
        }
        else
        {
            btnAll2 = button;
            btnAll2->setChecked(true);
        }
        connect(button, &QPushButton::clicked, this, [this, year]() { onYearClicked(year); });
    }
    mainLayout->addLayout(yearLayout);

//    // 第四行：排序
//    QStringList sorts = {"fanju", "jucb", "xiaojuc/fanwai"};
//    QHBoxLayout *sortLayout = new QHBoxLayout;
//    for (const QString &sort : sorts) {
//        QPushButton *button = new QPushButton(sort);
//        button->setStyleSheet("QPushButton { border: none; background-color: white; }"
//                              "QPushButton:hover { background-color: lightblue; }");
//        sortLayout->addWidget(button);
//        connect(button, &QPushButton::clicked, this, [this, sort]() { onCategoryClicked(sort); });
//    }
//    mainLayout->addLayout(sortLayout);

    // 列表视图
//    listWidget = new QListWidget;
//    mainLayout->addWidget(listWidget);

    AnimeView *view = new AnimeView();
    mainLayout->addWidget(view);

    setWindowTitle("分类和年份");
    resize(1200, 600); // 根据按钮数量和列表视图调整窗口大小


    connect(m_tagGroup, QOverload<int>::of( &QButtonGroup::buttonClicked), this, [=](int id)
    {
        m_tagId = id;
        QStringList tags;
        auto btns = m_tagGroup->buttons();
        for(auto p : btns)
        {
            if(p->isChecked())
            {
                tags.append(p->text());
            }
        }

        QStringList years;
        btns = m_yearGroup->buttons();
        for(auto p : btns)
        {
            if(p->isChecked())
            {
                years.append(p->text());
            }
        }

        if(tags.isEmpty())
        {
            btnAll1->setChecked(true);
        }
        else
        {
            btnAll1->setChecked(false);
        }


        Core::Filter filter;
        filter.tag = tags;
        filter.year = years;

        FlowControler::Interface->browseByTag(filter);
    });

    connect(m_yearGroup, QOverload<int>::of( &QButtonGroup::buttonClicked), this, [=](int id)
    {
        m_yearId = id;
        QStringList tags;
        auto btns = m_tagGroup->buttons();
        for(auto p : btns)
        {
            if(p->isChecked())
            {
                tags.append(p->text());
            }
        }

        QStringList years;
        btns = m_yearGroup->buttons();
        for(auto p : btns)
        {
            if(p->isChecked())
            {
                years.append(p->text());
            }
        }

        if(years.isEmpty())
        {
            btnAll2->setChecked(true);
        }
        else
        {
            btnAll2->setChecked(false);
        }

        Core::Filter filter;
        filter.tag = tags;
        filter.year = years;

        FlowControler::Interface->browseByTag(filter);
    });

    connect(FlowControler::Interface, &XInterface::getFilterAnimeFinished, this, [=]()
    {

    });
}

void List::onCategoryClicked(const QString &category) {
//    listWidget->clear(); // 清空列表
//    listWidget->addItem(QString("%1").arg(category)); // 添加选中的分类到列表
    // 这里可以添加更多的逻辑来根据分类更新列表内容

    if(category == "全部")
    {
        m_tagGroup->blockSignals(true);
        auto btns = m_tagGroup->buttons();
        for(auto p : btns)
        {
            p->setChecked(false);
        }
        m_tagGroup->blockSignals(false);

        QStringList years;
        btns = m_yearGroup->buttons();
        for(auto p : btns)
        {
            if(p->isChecked())
            {
                years.append(p->text());
            }
        }

        Core::Filter filter;
        filter.year = years;
        FlowControler::Interface->browseByTag(filter);
    }

}

void List::onYearClicked(const QString &category)
{
    if(category == "全部")
    {
        QStringList tags;
        auto btns = m_tagGroup->buttons();
        for(auto p : btns)
        {
            if(p->isChecked())
            {
                tags.append(p->text());
            }
        }

        m_yearGroup->blockSignals(true);
        btns = m_yearGroup->buttons();
        for(auto p : btns)
        {
            p->setChecked(false);
        }
        m_yearGroup->blockSignals(false);

        Core::Filter filter;
        filter.tag = tags;
        FlowControler::Interface->browseByTag(filter);
    }
}
